/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.core;

import java.util.zip.ZipFile;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.dltk.internal.core.util.Util;
import org.eclipse.php.core.INamespaceFragment;
import org.eclipse.php.core.INamespaceFragmentRoot;
import org.eclipse.php.core.IPharEntryResource;


@SuppressWarnings("restriction")
public abstract class PharEntryResource extends PlatformObject implements IPharEntryResource {

	protected Object parent;
	protected String simpleName;

	public PharEntryResource(String simpleName) {
		this.simpleName = simpleName;
	}

	public abstract PharEntryResource clone(Object newParent);

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof PharEntryResource))
			return false;
		PharEntryResource other = (PharEntryResource) obj;
		return this.parent.equals(other.parent) && this.simpleName.equals(other.simpleName);
	}

	protected String getEntryName() {
		String parentEntryName;
		if (this.parent instanceof INamespaceFragment) {
			String elementName = ((INamespaceFragment) this.parent).getElementName();
			parentEntryName = elementName.length() == 0 ? "" : elementName.replace('.', '/') + '/'; //$NON-NLS-1$
		}
		else if (this.parent instanceof INamespaceFragmentRoot) {
			parentEntryName = ""; //$NON-NLS-1$
		}
		else {
			parentEntryName = ((PharEntryDirectory) this.parent).getEntryName() + '/';
		}
		return parentEntryName + this.simpleName;
	}

	public IPath getFullPath() {
		return new Path(getEntryName()).makeAbsolute();
	}

	public String getName() {
		return this.simpleName;
	}

	public Object getParent() {
		return this.parent;
	}

	public INamespaceFragmentRoot getNamespaceFragmentRoot() {
		if (this.parent instanceof INamespaceFragment) {
			return (INamespaceFragmentRoot) ((INamespaceFragment) this.parent).getParent();
		}
		else if (this.parent instanceof INamespaceFragmentRoot) {
			return (INamespaceFragmentRoot) this.parent;
		}
		else {
			return ((PharEntryDirectory) this.parent).getNamespaceFragmentRoot();
		}
	}

	protected ZipFile getZipFile() throws CoreException {
		if (this.parent instanceof INamespaceFragment) {
			PharNamespaceFragmentRoot root = (PharNamespaceFragmentRoot) ((INamespaceFragment) this.parent).getParent();
			return root.getPhar();
		}
		else if (this.parent instanceof PharNamespaceFragmentRoot) {
			return ((PharNamespaceFragmentRoot) this.parent).getPhar();
		}
		else
			return ((PharEntryDirectory) this.parent).getZipFile();
	}

	@Override
	public int hashCode() {
		return Util.combineHashCodes(this.simpleName.hashCode(), this.parent.hashCode());
	}

	public boolean isReadOnly() {
		return true;
	}

	public void setParent(Object parent) {
		this.parent = parent;
	}
}
